package com.forg.mvc.core;

import java.util.HashMap;
import java.util.Map;

public class RequestParam {

    Map<String, Object> param = new HashMap<>();

    public Object get(String key) {

        return param.get(key);
    }

    public void put(String key, Object val) {

        param.put(key, val);
    }
    
    public void putAll(Map<? extends String, ? extends Object> m) {

        param.putAll(m);
    }

    public boolean isEmpty() {

        return param.isEmpty();
    }

    public void clear() {

        param.clear();
    }
    
    public String getStr(String key) {

        Object val = get(key);
        
        if(val == null) return null;
        
        return val.toString();
    }

    public Integer getInt(String key) {
        Object val = get(key);
        if(val == null ) return null;
        
        if(val instanceof Integer){
            return (Integer) val;
        }
        
        if(val  instanceof Number){
            return ((Number) val).intValue();
        }
        
        if (val instanceof String) {
            String strVal = (String) val;
            if (strVal.length() == 0) {
                return null;
            }

            return Integer.valueOf(strVal);
        }
        
        throw new RuntimeException("can not cast to int, value : " + val);
    }

    public int getIntVal(String key) {
        Integer val = getInt(key);
        if (val == null)
            return 0;

        return val.intValue();
    }

    public Long getLong(String key) {
        Object val = get(key);
        
        if(val == null) return null;

        if (val instanceof Number) {
            return ((Number) val).longValue();
        }

        if (val instanceof String) {
            String strVal = (String) val;
            if (strVal.length() == 0) {
                return null;
            }

            return Long.valueOf(strVal);
        }
        
        throw new RuntimeException("can not cast to long, value : " + val);
    }

    public long getLongVal(String key) {
        Long val = getLong(key);
        if (val == null)
            return 0l;

        return val.longValue();
    }

    public Double getDouble(String key) {
        Object val = get(key);

        if (val == null) return null;

        if (val instanceof Number) {
            return ((Number) val).doubleValue();
        }

        if (val instanceof String) {
            String strVal = val.toString();
            if (strVal.length() == 0) {
                return null;
            }
            return Double.valueOf(strVal);
        }
        
        throw new RuntimeException("can not cast to double, value : " + val);
    }

    public double getDoubleVal(String key) {
        Double val = getDouble(key);
        if (val == null)
            return 0d;

        return val.doubleValue();
    }

    public Float getFloat(String key) {
        Object val = get(key);

        if(val == null) return null;
        
        if (val instanceof Number) {
            return ((Number) val).floatValue();
        }

        if (val instanceof String) {
            String strVal = val.toString();
            if (strVal.length() == 0) {
                return null;
            }

            return Float.valueOf(strVal);
        }
        
        throw new RuntimeException("can not cast to float, value : " + val);
    }

    public float getFloatVal(String key) {
        Float val = getFloat(key);
        if (val == null)
            return 0f;

        return val.floatValue();
    }
    
    @Override
    public String toString(){
        
        return param.toString();
    }
}
